package com.xb.cloud.product.merchant.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.product.merchant.service.client.ShopService;
import com.xb.cloud.product.merchant.service.client.bo.ShopBo;
import com.xb.cloud.product.merchant.service.client.bo.ShopPromotionSkuBo;
import com.xb.cloud.product.service.business.model.Company;
import com.xb.cloud.product.service.business.model.Shop;
import com.xb.cloud.product.service.business.model.ShopExample;
import com.xb.cloud.product.service.business.model.ShopPromotionSku;
import com.xb.cloud.product.service.business.model.ShopPromotionSkuExample;
import com.xb.cloud.product.service.business.model.ShopExample.Criteria;
import com.xb.cloud.product.service.business.model.mapper.ShopMapper;
import com.xb.cloud.product.service.business.model.mapper.ShopPromotionSkuMapper;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * 
 * 店铺服务类
 *
 */
@Service
public class ShopServiceImpl implements ShopService {

	@Resource
	ShopMapper shopMapper;

	@Resource
	private ShopPromotionSkuMapper shopPromotionSkuMapper;

	@Override
	@Deprecated
	public ShopBo getById(Long id) {
		Shop shop = shopMapper.selectByPrimaryKey(id);
		ShopBo bo = new ShopBo();
		BeanUtils.copyProperties(shop, bo);
		return bo;
	}

	@Override
	@Deprecated
	public List<ShopBo> getShopListByIds(List<Long> ids) {
		ShopExample example = new ShopExample();
		example.createCriteria().andIdIn(ids);
		return shopMapper.getShopListByIds(example);
	}

	/**
	 * 根据shop id 查询
	 */
	@Override
	public ServiceReturnInfo getShopBoById(Long id) {
		ShopExample shopExample = new ShopExample();
		Criteria criteria = shopExample.createCriteria();
		criteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
		criteria.andIdEqualTo(id);
		List<Shop> shopList = shopMapper.selectByExample(shopExample);

		ShopBo shopBo = null;
		if (shopList != null && shopList.size() > 0) {
			shopBo = BeanUtils.newAndCopyProperties(ShopBo.class, shopList.get(0));
		}
		return ServiceReturnUtils.success(shopBo);
	}

	/**
	 * 根据shop id list 查询
	 */
	@Override
	public ServiceReturnInfo getShopBoListByIds(List<Long> ids) {
		ShopExample example = new ShopExample();
		example.createCriteria().andIdIn(ids);
		List<ShopBo> shopList = shopMapper.getShopListByIds(example);
		return ServiceReturnUtils.success(shopList);
	}

	/**
	 * 获取当前店铺的推荐列表
	 */
	@Override
	public ServiceReturnInfo getShopPromotionSkuList(Long shopId) {
		ShopPromotionSkuExample example = new ShopPromotionSkuExample();
		example.createCriteria().andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE).andIsEnabledEqualTo(DbConstants.TRUE)
				.andShopIdEqualTo(shopId);
		List<ShopPromotionSku> list = shopPromotionSkuMapper.selectByExample(example);
		return ServiceReturnUtils.success(list);
	}

	@Override
	public ServiceReturnInfo isValidShop(Long shopId) {
		Shop shop = shopMapper.selectByPrimaryKey(shopId);
		if (shop == null) {
			return ServiceReturnUtils.success(false);
		}
		if (DbConstants.isTrue(shop.getIsDeleted()) || !DbConstants.isTrue(shop.getIsEnabled())) {
			return ServiceReturnUtils.success(false);
		}
		if (DbConstants.SHOP_RUNNING != shop.getShopStatus()
				&& DbConstants.PLANTFORM_SHOP_RUNNING != shop.getCheckStatus()) {
			return ServiceReturnUtils.success(false);
		}
		return ServiceReturnUtils.success(true);
	}

}
